package com.example.question.bt.backtrack1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Date 2023-07-16
 * @Author Jonathan
 */
public class Code08_90 {
    public static void main(String[] args) {

    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null) {
            return res;
        }
        Arrays.sort(nums);
        dfs(nums, 0, new ArrayList<>(), res);
        return res;
    }

    private void dfs(int[] nums, int startIndex, List<Integer> subset, List<List<Integer>> res) {
        res.add(new ArrayList<>(subset));
        for (int i = startIndex; i < nums.length; i++) {
            if (i > startIndex && nums[i] == nums[i - 1]) {
                // 剪枝
                continue;
            }
            subset.add(nums[i]);
            dfs(nums, startIndex + 1, subset, res);
            // 回溯的时候 删除最后一个节点
            subset.remove(subset.size() - 1);
        }
    }


}
